package com.android.baseconfig.data.http.okhttp.interceptor;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.constact.AppGlobalHost;
import com.android.baseconfig.data.http.okhttp.listener.NetworkLoadingListener;
import com.android.baseconfig.data.http.okhttp.util.OkHttpTag;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by uuxia-mac on 16/7/10.
 */
public class HeTNetworkLoadingInterceptor implements Interceptor {

    private NetworkLoadingListener loadingListener;

//    private OkHttpTag okHttpTag;

    private ConcurrentHashMap<String, OkHttpTag> okHttpMap = new ConcurrentHashMap<String, OkHttpTag>();

    private Handler mDelivery;

    private ConcurrentHashMap<Object, Object> dialogs = new ConcurrentHashMap<>();

    public HeTNetworkLoadingInterceptor() {
        mDelivery = new Handler(Looper.getMainLooper());
    }

    public HeTNetworkLoadingInterceptor(NetworkLoadingListener progressListener) {
        this();
        this.loadingListener = progressListener;
    }

    public void setOkHttpTag(OkHttpTag okHttpTag) {
        if (okHttpTag != null) {
            okHttpMap.put(TextUtils.isEmpty(okHttpTag.getPath()) ? "" : okHttpTag.getPath(), okHttpTag);
        }
    }

    public void setLoadingListener(NetworkLoadingListener loadingListener) {
        this.loadingListener = loadingListener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = null;
        final Request request = chain.request();
        try {
            if (request != null) {
                String path = getPath(request);
                if (!TextUtils.isEmpty(path)) {
                    final OkHttpTag okHttpTag = okHttpMap.get(path);
                    if (null != loadingListener && null != okHttpTag && okHttpTag.canShowDialog()) {
                        mDelivery.post(new Runnable() {
                            @Override
                            public void run() {
                                Object dlg = loadingListener.showLoading(okHttpTag.getActivity(), okHttpTag.getShowMsg());
                                if (dlg != null) {
                                    dialogs.put(request, dlg);
                                }
                            }
                        });
                    }
                }
            }
            response = chain.proceed(request);
        } catch (Exception e) {
            throw e;
        } finally {
            if (request != null && loadingListener != null) {
                String path = getPath(request);
                if (!TextUtils.isEmpty(path)) {
                    OkHttpTag okHttpTag = okHttpMap.remove(path);
                    if (okHttpTag != null && okHttpTag.getActivity() != null) {
                        mDelivery.post(new Runnable() {
                            @Override
                            public void run() {
                                if (request != null) {
                                    Object dialog = dialogs.remove(request);
                                    if (dialog != null) {
//                                    loadingListener.hideLoading(okHttpTag.getActivity(), dialog);
                                        loadingListener.hideLoading(dialog);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
        return response;
    }


    /*@Override
    public Response intercept(Chain chain) {
        Response response = null;
        try {
            Request request = chain.request();
            if (request != null) {
                String path = getPath(request);
                if (!TextUtils.isEmpty(path)) {
                    OkHttpTag okHttpTag = okHttpMap.get(path);
                    if (null != loadingListener && null != okHttpTag && okHttpTag.canShowDialog()) {
                        mDelivery.post(() -> {
                            Object dlg = loadingListener.showLoading(okHttpTag.getActivity(), okHttpTag.getShowMsg());
                            if (dlg != null) {
                                dialogs.put(request, dlg);
                            }
                        });
                    }
                }
            }

            response = chain.proceed(request);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Request request = chain.request();
            if (request != null && loadingListener != null) {
                String path = getPath(request);
                if (!TextUtils.isEmpty(path)) {
                    OkHttpTag okHttpTag = okHttpMap.remove(path);
                    if (okHttpTag != null && okHttpTag.getActivity() != null) {
                        mDelivery.post(() -> {
                            if (request != null) {
                                Object dialog = dialogs.remove(request);
                                if (dialog != null) {
//                                    loadingListener.hideLoading(okHttpTag.getActivity(), dialog);
                                    loadingListener.hideLoading(dialog);
                                }
                            }
                        });
                    }
                }
            }
//            if (null != loadingListener && null != okHttpTag && okHttpTag.canShowDialog()) {
//                okHttpTagSet.remove(okHttpTag);
//                mDelivery.post(() -> {
//                    Request request = chain.request();
//                    if (request != null) {
//                        Object dialog = dialogs.remove(request);
//                        loadingListener.hideLoading(okHttpTag.getActivity(), dialog);
//                    }
//                });
//            }
        }
        return response;
    }*/

    private String getPath(Request request) {
        HttpUrl httpUrl = request.url();
        if (httpUrl.pathSize() > 0) {
//            String path = httpUrl.pathSegments().get(0);
            String path = httpUrl.uri().getPath().replace(AppGlobalHost.getHost(), "");
            Logc.d(path);
            return path;
        }
        return null;
    }
}
